package gogogo.liwushuo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.SpLeftRvAdapter;
import gogogo.liwushuo.adapter.SpRightRvAdapter;
import gogogo.liwushuo.bean.SingleProductBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by cong on 2016/7/20.
 */
public class SingleProductFragment extends Fragment {
    @InjectView(R.id.sp_rv_left)
    RecyclerView spRvLeft;
    @InjectView(R.id.sp_rv_right)
    RecyclerView spRvRight;
    private List<SingleProductBean.DataBean.CategoriesBean> mList;
    private SpLeftRvAdapter spLeftRvAdapter;
    private SpRightRvAdapter spRightRvAdapter;
    private Context mContext;
    private Handler handler;
    private View lastView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = new ArrayList<>();
        mContext = getActivity();
        spLeftRvAdapter = new SpLeftRvAdapter(mContext, mList, lastView);
        spRightRvAdapter = new SpRightRvAdapter(mContext, mList);
        handler = new Handler();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup
            container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_singleproduct, null);
        ButterKnife.inject(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        spRvLeft.setLayoutManager(new LinearLayoutManager(mContext));
        spRvLeft.setAdapter(spLeftRvAdapter);
        spRvRight.setLayoutManager(new LinearLayoutManager(mContext));
        spRvRight.setAdapter(spRightRvAdapter);
        spLeftRvAdapter.setOnItemClickLisener(new SpLeftRvAdapter.OnItemClickLisener() {
                                                  public void onItemClickListener(View view, int position) {
                                                      if (lastView != view) {
                                                          view.setBackgroundColor(getResources().getColor(R.color.colorWhite));
                                                          view.findViewById(R.id.sp_left_ll).setVisibility(View.VISIBLE);
                                                          ((TextView) view.findViewById(R.id.sp_left_tv)).setTextColor(getResources().getColor(R.color.colorToolbar));
                                                          if (lastView != null) {
                                                              lastView.setBackgroundColor(getResources().getColor(R.color.categorySearchColor));
                                                              lastView.findViewById(R.id.sp_left_ll).setVisibility(View.INVISIBLE);
                                                              ((TextView) lastView.findViewById(R.id.sp_left_tv)).setTextColor(getResources().getColor(R.color.colorBlack));
                                                          }
                                                          LinearLayoutManager manager = new LinearLayoutManager(mContext);
                                                          manager.scrollToPosition(position);
                                                          spRvRight.setLayoutManager(manager);
                                                          lastView = view;
                                                      }
                                                  }
                                              }

        );
        spRvRight.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                RecyclerView.LayoutManager manager = spRvRight.getLayoutManager();
                int first = 0;
                if (dy > 0) {
                    first = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
                } else {
                    first = ((LinearLayoutManager) manager).findFirstVisibleItemPosition();
                }
                LinearLayoutManager leftManager = (LinearLayoutManager) spRvLeft.getLayoutManager();
                if (first >= leftManager.findLastVisibleItemPosition()) {
                    spRvLeft.scrollToPosition(first);
                }
                if (first <= leftManager.findFirstVisibleItemPosition()) {
                    spRvLeft.scrollToPosition(first);
                }
                View child = spRvLeft.getChildAt(first - leftManager.findFirstVisibleItemPosition());
                if (lastView != child && child != null) {
                    child.setBackgroundColor(getResources().getColor(R.color.colorWhite));
                    child.findViewById(R.id.sp_left_ll).setVisibility(View.VISIBLE);
                    ((TextView) child.findViewById(R.id.sp_left_tv)).setTextColor(getResources().getColor(R.color.colorToolbar));
                    if (lastView != null) {
                        lastView.setBackgroundColor(getResources().getColor(R.color.categorySearchColor));
                        lastView.findViewById(R.id.sp_left_ll).setVisibility(View.INVISIBLE);
                        ((TextView) lastView.findViewById(R.id.sp_left_tv)).setTextColor(getResources().getColor(R.color.colorBlack));
                    }
                    lastView = child;
                }
            }
        });
        downLoad();
    }

    private void downLoad() {
        OKHttpUtils.newInstance(mContext).getAsyncData(Resource.URL_CATEGORY_SECOND, new Callback() {
            public void onFailure(Call call, IOException e) {

            }

            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                SingleProductBean bean = new Gson().fromJson(string, SingleProductBean.class);
                mList.addAll(bean.getData().getCategories());
                handler.post(new Runnable() {
                    public void run() {
                        spLeftRvAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
